package com.itluma.myshopping.dao.impl;

import com.itluma.myshopping.dao.UserDao;
import com.itluma.myshopping.entity.User;
import com.itluma.myshopping.utils.C3P0Utils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class UserDaoImpl implements UserDao {
    private QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
    private Map<String,String> map=new HashMap<String, String>();
    {
        map.put("u_id","uid");
        map.put("u_name","username");
        map.put("u_password","upassword");
        map.put("u_sex","usex");
        map.put("u_status","ustatus");
        map.put("u_code","code");
        map.put("u_email","email");
        map.put("u_role","urole");
    }
    @Override
    public User findByName(String username) throws SQLException {
        String sql="select * from user where u_name=?";
        User query = qr.query(sql, new BeanHandler<User>(User.class,new BasicRowProcessor(new BeanProcessor(map))),username);

        return query;
    }

    public static void main(String[] args) throws SQLException {
        UserDao ud=new UserDaoImpl();
        User admin = ud.findByName("admin");
        System.out.println(admin);
    }
}
